from Common.CommonUtils import CommonUtils
from po.Disciplines import Disciplines
from po.Person import Person


class ExcelForm:

    def __init__(self, filePath):

        self.filePath = filePath

        # 创建一个空列表来存储每一行的数据
        self.rows_data = []

        # 学生列表
        self.Person = []

        # 学期时间
        self.SemesterTime_start = ""

        # 截止时间
        self.SemesterTime_shop = ""

        # 一 || 二 学期
        self.middle = ""

    def extract(self):

        # 遍历工作表的所有行
        self.rows_data = CommonUtils.table_extract(self.filePath)

        person = None
        allDisciplinesNum = []
        key = 0
        for i, row in enumerate(self.rows_data):
            # print(f"Row {i + 1}: {row}")

            if i == 0 or i == 1 or i == 2:
                continue

            if "班级名称" in row[0] and "学号" in row[0]:
                # key = True

                person = Person()
                allDisciplinesNum = []

                # print(f"下面的是一位学生的消息:{row}")
                # 使用 split() 方法按空格分割字符串
                split_string = row[0].split()
                dictionary = {}
                for str in split_string:
                    split = str.split("：")
                    dictionary[split[0]] = split[1]

                person.set_grade(dictionary["班级名称"])
                person.set_studentId(dictionary["学号"])
                person.set_name(dictionary["姓名"])

            elif "累计学分" in row[0]:
                person.set_allDisciplinesNum(allDisciplinesNum)
                # print(f"这是最后一行:{row}")
                dictionary = {}
                for str in row:
                    if str != "":
                        split = str.split("：")
                        dictionary[split[0]] = split[1]
                person.set_allCredits(dictionary["累计学分"])
                person.set_disciplinesNum(dictionary["当前小计"])
                self.Person.append(person)
            elif "院系盖章" in row[0]:
                print("读取完毕")

            else:
                dictionary = Disciplines()
                dictionary.set_subjectName(row[0])
                dictionary.set_subjectCredit(row[2])
                dictionary.set_subjectGrades(row[5])
                self.handlingTime(row[4])
                person.set_SemesterTime_start(self.SemesterTime_start)
                person.set_SemesterTime_shop(self.SemesterTime_shop)
                person.set_middle(self.middle)
                allDisciplinesNum.append(dictionary)

        return self.Person

    def handlingTime(self, time):
        times = time.split("-")
        if self.SemesterTime_start == "" and self.SemesterTime_shop == "":
            self.SemesterTime_start = times[0]
            self.SemesterTime_shop = times[1]
            if times[2] == "1":
                self.middle = "一"
            elif times[2] == "2":
                self.middle = "二"
            return
        elif int(times[0]) < int(self.SemesterTime_start):
            self.SemesterTime_start = times[0]
        if int(times[1]) > int(self.SemesterTime_shop):
            self.SemesterTime_shop = times[1]
            if times[2] == "1":
                self.middle = "一"
            elif times[2] == "2":
                self.middle = "二"
